package main.剑指OfferⅠ;

public class Day11 {
    public static void main(String[] args) {
        System.out.println("剑指Offer18.删除链表的节点");
        System.out.println("剑指Offer22.链表中倒数第K个节点");
    }
}

class Offer18{
    public ListNode deleteNode(ListNode head, int val) {
        ListNode vir=new ListNode(0),cur=head,pre=vir;
        pre.next=head;
        while (cur!=null){
            if (cur.val==val){
                pre.next=cur.next;
                break;
            }
            cur=cur.next;
            pre=pre.next;
        }
        return vir.next;
    }
}

class Offer22{
    public ListNode getKthFromEnd(ListNode head, int k) {
        ListNode temp=head,result=head;
        int counts=0;//总节点个数
        while (temp!=null) {
            counts++;
            temp=temp.next;
        }
        int off=counts-k;
        while (off!=0){
            off--;
            result=result.next;
        }
        return result;
    }
}